package com.linkallcloud.core.query.rule.desc;

import com.alibaba.fastjson2.JSONWriter;
import com.alibaba.fastjson2.annotation.JSONField;
import com.linkallcloud.core.query.Operator;

public class ObjectRuleDescriptor extends RuleDescriptor {

    @JSONField(serializeFeatures = JSONWriter.Feature.WriteEnumsUsingName)
    private Operator op;// 操作

    private Object data;

    public ObjectRuleDescriptor() {
        super();
    }

    public ObjectRuleDescriptor(String field, Object op, Object data) {
        super(field, null);
        this.setOp(op);
        this.data = data;
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.linkallcloud.dao.query.rule.desc.IRuleDescriptor#getOperator()
     */
    public Operator getOperator() {
        return op;
    }

    /**
     * @return the op
     */
    // @JSONField(serializeFeatures = JSONWriter.Feature.WriteEnumsUsingName)
    public Operator getOp() {
        return op;
    }

    /**
     * @param op
     *            the op to set
     */
    public void setOp(Object op) {
        if (op != null) {
            if (op instanceof Operator) {
                this.op = (Operator) op;
            } else if (op instanceof String) {
                try {
                    this.op = Enum.valueOf(Operator.class, (String) op);
                } catch (Throwable e) {
                    throw new IllegalArgumentException("op(" + op + ") not support.", e);
                }
            } else if (op instanceof Integer) {
                boolean ok = false;
                int opIdx = (Integer) op;
                Operator[] list = Operator.values();
                for (Operator o : list) {
                    if (o.ordinal() == opIdx) {
                        this.op = o;
                        ok = true;
                        break;
                    }
                }
                if (!ok) {
                    throw new IllegalArgumentException("op idx(" + opIdx + ") error.");
                }
            } else {
                throw new IllegalArgumentException("op must not null.");
            }
        } else {
            throw new IllegalArgumentException("op must not null.");
        }
    }

    /**
     * 
     * @return
     */
    public Object getData() {
        return data;
    }

    /**
     * @param data
     *            the data to set
     */
    public void setData(Object data) {
        this.data = data;
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.linkallcloud.dao.query.rule.desc.IRuleDescriptor#getValue()
     */
    @Override
    public Object getValue() {
        return data;
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.linkallcloud.dao.query.rule.desc.IRuleDescriptor#setValue(java.lang.Object)
     */
    @Override
    public void setValue(Object value) {
        this.data = value;
    }

}
